package com.mmall.service.impl;

import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.mmall.common.Const;
import com.mmall.common.ResponseCode;
import com.mmall.common.ServiceResponse;
import com.mmall.dao.CartMapper;
import com.mmall.dao.ProductMapper;
import com.mmall.pojo.Cart;
import com.mmall.pojo.Product;
import com.mmall.service.ICartService;
import com.mmall.util.BigDecimalUtil;
import com.mmall.util.PropertiesUtil;
import com.mmall.vo.CartProductVo;
import com.mmall.vo.CartVo;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by sc on 2017/8/29.
 */
@Service("iCartService")
public class CartServiceImpl implements ICartService {

    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private ProductMapper productMapper;

    @Override
    public ServiceResponse<CartVo> add(Integer userid, Integer productId, Integer count) {
        if (userid == null || productId == null || count == null) {
            return ServiceResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }

        Cart cart = cartMapper.selectCartByUserIDandProductID(userid, productId);
        if (cart == null) {
            //产品不再购物车中需要新增一个
            Cart cartitem = new Cart();
            cartitem.setQuantity(count);
            cartitem.setChecked(Const.Cart.CHECKED);
            cartitem.setProductId(productId);
            cartitem.setUserId(userid);
            cartMapper.insert(cartitem);
        } else {
            count = count + cart.getQuantity();
            cart.setQuantity(count);
            cartMapper.updateByPrimaryKeySelective(cart);
        }
        return this.list(userid);
    }

    @Override
    public ServiceResponse<CartVo> update(Integer userid, Integer productId, Integer count) {
        if (userid == null || productId == null || count == null) {
            return ServiceResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        Cart cart = cartMapper.selectCartByUserIDandProductID(userid, productId);
        if (cart != null) {
            cart.setQuantity(count);
        }
        cartMapper.updateByPrimaryKeySelective(cart);
        return this.list(userid);
    }

    @Override
    public ServiceResponse<CartVo> delete(Integer userid, String productids) {
        //
        if (userid == null || productids == null) {
            return ServiceResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        List<String> productList = Splitter.on(",").splitToList(productids);
        cartMapper.deleteByUserIDandProductIds(userid, productList);
        return this.list(userid);
    }

    @Override
    public ServiceResponse<CartVo> list(Integer userid) {
        CartVo cartVo = this.getCartVoLimit(userid);
        return ServiceResponse.createBySuccess(cartVo);
    }

    @Override
    public ServiceResponse<CartVo> selectorUnselect(Integer userid, Integer checked, Integer productid) {
        if (userid == null) {
            return ServiceResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        cartMapper.checkedorUnchecked(userid, checked, productid);
        return this.list(userid);
    }

    @Override
    public ServiceResponse<Integer> getCartProductCount(Integer userid) {
        if (userid == null) {
            return ServiceResponse.createBySuccess(0);
        }
        return ServiceResponse.createBySuccess(cartMapper.getCartProductCount(userid));
    }


    private CartVo getCartVoLimit(Integer userid) {
        CartVo cartVo = new CartVo();
        List<Cart> cartList = cartMapper.selectCartByUserId(userid);
        List<CartProductVo> cartProductVoList = Lists.newArrayList();
        BigDecimal cartTotalPrice = new BigDecimal("0");//初始化总价
        if (CollectionUtils.isNotEmpty(cartList)) {
            for (Cart cartitem : cartList) {
                CartProductVo cartProductVo = new CartProductVo();
                cartProductVo.setId(cartitem.getId());
                cartProductVo.setUserid(userid);
                cartProductVo.setProductid(cartitem.getProductId());
                Product product = productMapper.selectByPrimaryKey(cartitem.getProductId());
                if (product != null) {
                    cartProductVo.setProductMainImage(product.getMainImage());
                    cartProductVo.setProductname(product.getName());
                    cartProductVo.setSubtitle(product.getSubtitle());
                    cartProductVo.setStatus(product.getStatus());
                    cartProductVo.setPrice(product.getPrice());
                    cartProductVo.setProductStock(product.getStock());
                    int buylimit = 0;
                    if (product.getStock() >= cartitem.getQuantity()) {
                        buylimit = cartitem.getQuantity();
                        cartProductVo.setLimitQuantity(Const.Cart.LIMIT_NUM_SUCCESS);
                    } else {
                        buylimit = product.getStock();
                        cartProductVo.setLimitQuantity(Const.Cart.LIMIT_NUM_FAIL);
                        Cart cartforQuantity = new Cart();
                        cartforQuantity.setId(cartitem.getId());
                        cartforQuantity.setQuantity(buylimit);
                        cartMapper.updateByPrimaryKeySelective(cartforQuantity);
                    }
                    cartProductVo.setQuantity(buylimit);
                    //某种产品计算总价
                    cartProductVo.setProductTotalPrice(BigDecimalUtil.mul(product.getPrice().doubleValue(), cartProductVo.getQuantity()));
                    cartProductVo.setProductChecked(cartitem.getChecked());
                }
                if (cartitem.getChecked() == Const.Cart.CHECKED) {
                    //勾选的增加到购物车总价
                    cartTotalPrice = BigDecimalUtil.add(cartTotalPrice.doubleValue(), cartProductVo.getProductTotalPrice().doubleValue());
                }
                cartProductVoList.add(cartProductVo);
            }
        }
        cartVo.setCartTotalPrice(cartTotalPrice);
        cartVo.setCartProductVo(cartProductVoList);
        cartVo.setAllChecked(this.getAllCheckedStatus(userid));
        cartVo.setImageHost(PropertiesUtil.getProperty("http://img.happymall.com/"));
        return cartVo;
    }

    private boolean getAllCheckedStatus(Integer userid) {
        if (userid == null) {
            return false;
        }
        return cartMapper.selectProductCheckedByUserId(userid) == 0;
    }


}
